import Opt from '@/components/dt/info/Opt.js'
import organizationOpt from '@/biz/option/organization.js'
import houseOpt from '@/biz/option/house.js'
import ruleOpt from '@/biz/option/rule.js'
import tool from '@/common/tool.js'

class SearchOpt extends Opt {
  constructor(info) {
    super()
    this.info = info
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [
      organizationOpt.create({
        key: 'orgIds',
        level: 3
      }),
      houseOpt.create({
        to: (info) => {
          return {
            housesId: info.housesId
          }
        }
      }),
      {
        title: '案场状态',
        placeholder: '全部',
        key: 'status',
        select: {
          key: 'buildStatus',
          clearable: true
        }
      },
      {
        title: '日期范围',
        placeholder: '请选择',
        key: 'startDate',
        dtTime: {
          dateType: 'month',
          dateFormat: 'YYYY-MM',
          clearable: true,
          minimumDate: '2023-08-01'
        },
        data: {
          from: (info) => {
            if (info.startDate || info.endDate) {
              return [info.startDate, info.endDate]
            }
            return null
          },
          to: (val) => {
            return {
              startDate: val[0],
              endDate: val[1]
            }
          }
        },
        rules: [
          ruleOpt.check(
            '请选择日期范围',
            (val) => {
              return !val ^ !this.info.endDate
            },
            'change'
          ),
          ruleOpt.check(
            '日期范围最大跨度一年',
            (val) => {
              return tool.dateDiff(val, this.info.endDate).Y >= 1
            },
            'change'
          )
        ]
      }
    ]
  }
}

export default SearchOpt
